import * as Cesium from "cesium";

class geoJsonDataEffect {
    viewer: Cesium.Viewer;
    options: any;
    geoPromise: any
    dataSource: any
  constructor(viewer, options) {
    this.viewer = viewer;
    this.dataSource = null
    this.options = options
    this.init();
  }

  init() {
    this.geoPromise = Cesium.GeoJsonDataSource.load(this.options.data,{
        fill: Cesium.Color.TRANSPARENT,
        strokeWidth: 5
    });
  }

  destroy() {
    if (this.dataSource == null) {
        throw new Error('矢量数据未加载或已被销毁');
      }
  
      // 取消所有监听器
    //   this.dataSource.changedEvent.removeEventListener(this.changedEvent);
    //   this.dataSource.errorEvent.removeEventListener(this.errorEvent)
  
      // 移除数据源
      this.viewer.dataSources.remove(this.dataSource);
      this.dataSource = null;
      console.log('CesiumGeoJSON has been destroyed.');
  }

  show(visible:boolean) {
    if(visible) {
        this.geoPromise.then((dataSource) => {
            console.log(dataSource,'daas')
            this.dataSource = dataSource
            this.viewer.dataSources.add(this.dataSource)
            this.dataSource.name = this.options.name;
            this.viewer.flyTo(this.dataSource.entities.values)
        })
    }else{
        this.viewer.dataSources.remove(this.viewer.dataSources.getByName(this.options.name)[0])
    }
  }


}

export default geoJsonDataEffect;
